package com.markerhub.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.common.lang.Result;
import com.markerhub.common.lang.retunData;
import com.markerhub.entity.TFacilityBase;
import com.markerhub.entity.TPayRefund;
import com.markerhub.povo.TPayOrderVo;
import com.markerhub.povo.TPayRefundVo;
import com.markerhub.service.TPayRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘佳明
 * @since 2022-02-09
 */
@RestController
@RequestMapping("/tPayRefund")
public class TPayRefundController {


    @Autowired
    TPayRefundService tPayRefundService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:refund:list')")
    //@ResponseBody
    public retunData list(Integer page, Integer limit, String number , String orderNumber, String projectid, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date1,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date2, String defarystatus, Integer Userids) throws ParseException {

        //https://blog.csdn.net/jingsong2015/article/details/77714780
        // System.out.println(page);
        System.out.println(limit);
        System.out.println(projectid);
        System.out.println(orderNumber);
        System.out.println(defarystatus);

        LocalDate dateup = date1;
        LocalDate  datedown = date2;
        System.out.println(date1);
        System.out.println(date2);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd ");


        if (page==0&&limit==0){
            List<TPayRefundVo> list = tPayRefundService.getRefundList(0,0,number,orderNumber,projectid,dateup,datedown,defarystatus,Userids);
            retunData retunData1 = new retunData();
            retunData1.setData(list);
            return retunData1;
        }
        System.out.println(dateup+"LocalDateTime");
        System.out.println(datedown +"LocalDateTime");
        Integer pages=(page-1)*limit;

        List<TPayRefundVo> TFacilityBaseVoList= tPayRefundService.getRefundList(pages,limit,number,orderNumber,projectid,dateup,datedown,defarystatus,Userids);
        List<TPayRefundVo> count= tPayRefundService.getRefundList(0,0,number,orderNumber,projectid,dateup,datedown,defarystatus,Userids);
        // System.out.println("List<TFacilityBaseVo> count "+count);
        retunData retunData = new retunData();
        retunData.setData(TFacilityBaseVoList);
        retunData.setCount(count.size());


        return retunData;
    }


    @RequestMapping("/claim")
    @PreAuthorize("hasAuthority('sys:refund:add')")
    @ResponseBody
    public Result claim(String orderid, String  describe,String refundPhone ){

        System.out.println(orderid);
        System.out.println(describe);
        System.out.println(refundPhone);
        List<TPayRefund> orderid1 =  tPayRefundService.list(new QueryWrapper<TPayRefund>().eq("orderid", orderid));
        if (orderid1.size()>0){
            return  Result.fail("该订单已经过申请退款！！！");
        }
        TPayRefund tPayRefund = new TPayRefund();
        tPayRefund.setRefundDelete(1);
        tPayRefund.setOrderid(orderid);
        tPayRefund.setRefundTime(LocalDateTime.now());
        //PayRefund.setRefundStateTime(LocalDateTime.now());
        tPayRefund.setDescribes(describe);
        tPayRefund.setRefundPhone(refundPhone);
        tPayRefund.setRefundStatus(0);
        boolean save = tPayRefundService.save(tPayRefund);
        return  Result.succ(tPayRefund);



    }



}

